package com.stockz.equity.charting.view.widget;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;

import org.springframework.aop.target.dynamic.Refreshable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import com.stockz.equity.charting.event.EventPublisher;
import com.stockz.equity.charting.event.RepaintStockChartPanelEvent;
import com.stockz.equity.charting.painter.chart.StockChartPainter;
import com.stockz.equity.charting.util.ContextUtil;
import com.stockz.equity.charting.view.StockChartView;
import com.stockz.equity.charting.view.toolbar.ViewToolbar;
import com.stockz.equity.pattern.Child;

@Component
@Lazy
public class ChartTypeCombobox extends JComboBox implements Child,Refreshable{
	private static final long serialVersionUID = 6867171274365560371L;

	@SuppressWarnings("unchecked")
	private List<Class> parentList = new ArrayList<Class>(1);
	
	@Autowired StockChartView stockChartView;
	
	public ChartTypeCombobox() {
		super();
		parentList.add(ViewToolbar.class);
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public void refresh() {
		Map<String,StockChartPainter> chartPainterMap = ContextUtil.getApplicationContext().getBeansOfType(StockChartPainter.class);
		if(chartPainterMap != null && !chartPainterMap.isEmpty()){
			setModel(new DefaultComboBoxModel(chartPainterMap.values().toArray()));
			setSelectedIndex(2);
		}
		
		
		addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				stockChartView.getSelectedStockChart().getStockChartPanel().getStockChartPanelState()
				.setStockChartPainter((StockChartPainter)getSelectedItem());
				EventPublisher.publishEvent(
						new RepaintStockChartPanelEvent(
								stockChartView.getSelectedStockChart().getStockChartPanel()));
			}
		});
	}

	@SuppressWarnings("unchecked")
	@Override
	public Collection<Class> getParents() {
		return parentList;
	}

	@Override
	public long getLastRefreshTime() {
		return 0;
	}

	@Override
	public long getRefreshCount() {
		return 0;
	}
}
